package com.fingard.avatar.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fingard.avatar.website.dao.ExperienceApplyMapper;
import com.fingard.avatar.website.model.dto.ExperienceApplyAddDto;
import com.fingard.avatar.website.model.dto.ExperienceApplyPageDto;
import com.fingard.avatar.website.model.po.ExperienceApplyPo;
import com.fingard.avatar.website.service.ExperienceApplyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 *  Service实现类
 *
 * @author 徐一涛
 * @date 2022-05-11
 */
@Slf4j
@Service
public class ExperienceApplyServiceImpl extends ServiceImpl<ExperienceApplyMapper, ExperienceApplyPo> implements ExperienceApplyService {

    @Override
    public Boolean add(ExperienceApplyAddDto req) {
        ExperienceApplyPo bean = new ExperienceApplyPo();
        BeanUtils.copyProperties(req, bean);
        bean.setCreateTime(LocalDateTime.now());
        return save(bean);
    }

    @Override
    public IPage<ExperienceApplyPo> page(ExperienceApplyPageDto req) {
        LambdaQueryWrapper<ExperienceApplyPo> wrapper = Wrappers.<ExperienceApplyPo>lambdaQuery().
                orderByDesc(ExperienceApplyPo::getCreateTime);
        if(StrUtil.isNotEmpty(req.getName())) {
            wrapper.like(ExperienceApplyPo::getName, req.getName());
        }
        return page(new Page<>(req.getPageNum(), req.getPageSize()), wrapper);
    }
}
